$(function() {
    $('#Commonbottom').load('../assets/public/html/PublicBottom.html');
    $('.Published').mousedown(function() {
        $(this).addClass("orangeBack");
        $('.Deleted,.Employed').removeClass("orangeBack");
        $('.companyPosstion').css("display", "block").css("display", "flex");
        $('.comDeleted,.comEmployed').css("display", "none");
    });
    $('.Employed').mousedown(function() {
        $(this).addClass("orangeBack");
        $('.Deleted,.Published').removeClass("orangeBack");
        $('.comEmployed').css("display", "block").css("display", "flex");
        $('.comDeleted,.companyPosstion').css("display", "none");
    });
    $('.Deleted').mousedown(function() {
        $(this).addClass("orangeBack");
        $('.Employed,.Published').removeClass("orangeBack");
        $('.comDeleted').css("display", "block").css("display", "flex");
        $('.comEmployed,.companyPosstion').css("display", "none");
    });
    $('.leftContents>li').eq(0).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn1').css("display", "block");
    });
    $('.leftContents>li').eq(1).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn2').css("display", "block");
    });
    $('.leftContents>li').eq(2).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn3').css("display", "block");
    });
    $('.leftContents>li').eq(3).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn4').css("display", "block");
    });
    $('.leftContents>li').eq(4).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn5').css("display", "block");
    });
    $('.leftContents>li').eq(5).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn6').css("display", "block");

    });
    $('.leftContents>li').eq(6).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn7').css("display", "block");
    });
    $('.leftContents>li').eq(7).mousedown(function() {
        $(this).addClass("orange");
        var defa = $('li').not($(this));
        $(defa).removeClass("orange");
        $('.CompanyInformation>div').css("display", "none");
        $('.companyIn8').css("display", "block");
    });
    $('.changeNum').mousedown(function() {
        $('.companyIn7').css("display", "none");
        $('.conpanyIn7Changpasswd').css("display", "block");
    });
    $('.changepasswd').mousedown(function() {
        $('.companyIn7').css("display", "none");
        $('.companyIn7ChangeNum').css("display", "block");
    });

    $('.changephoto').mousedown(function() {
        $('.companyIn3').css("display", "none");
        $('.changcompany3').css("display", "block");
    });
    $('.changeProuce').mousedown(function() {
        $('.companyIn2').css("display", "none");
        $('.changcompany2').css("display", "block");
    });
    $('.changebenefits').mousedown(function() {
        $('.companyIn4').css("display", "none");
        $('.companyInBenefits').css("display", "block");
    });


    $('.verification').click(function() {
        let verFlag = false;
        const pattern = /^[1][3,4,5,7,8,9][0-9]{9}$/;;
        let phoneBer = $('#phone').val();
        if (!pattern.test(phoneBer)) {
            $('#phone').text("手机号格式有错误")
        } else {
            verFlag = true
        }
        if (verFlag) {
            let res = Interceptor('/send', 'post', {
                'phone': phoneBer
            })
        }
    });


    $('#submit').mousedown(function() {
        let phoneBer = $('#phone').val();
        const PhoneCode = $('#codeVal').val();
        console.log(phoneBer);
        console.log(PhoneCode);
        Interceptor('/modifyPhone', 'post', {
            'phone': phoneBer,
            'code': PhoneCode
        })

    })

    $(".thingIng").eq(0).click(function() {
        $(this).children("#Propic").click();
        $(this).children("#Propic").on("change", function() {
            var ImgRoute = window.URL.createObjectURL(this.files[0]);
            // console.log(ImgRoute);
            if (ImgRoute) {
                let proImg = $(".thingIng").eq(0).children().eq(0);
                let pClass = $(".thingIng").eq(0).children().eq(1);
                proImg.attr("src", ImgRoute);
                proImg.css("border-radius", "50%").css("width", "134px").css("height", "134px").css("display", "block");
                pClass.css("display", "none");
            }
        });
    });
    $(".thingIng").eq(1).click(function() {
        $(this).children("#Propic").click();
        $(this).children("#Propic").on("change", function() {
            var ImgRoute = window.URL.createObjectURL(this.files[0]);
            // console.log(ImgRoute);
            if (ImgRoute) {
                let proImg = $(".thingIng").eq(1).children().eq(0);
                let pClass = $(".thingIng").eq(1).children().eq(1);
                proImg.attr("src", ImgRoute);
                proImg.css("border-radius", "50%").css("width", "134px").css("height", "134px").css("display", "block");
                pClass.css("display", "none");
            }
        });
    });

    $(".changeWork").click(function() {
        $(".changcompany3").css("display", "none");
        $(".companyIn3").css("display", "block");
    });


    function changePhones() {
        $('.ChosesPassword').click(function() {
            let verFlag = false;
            const pattern = /^1(3|4|5|6|7|8|9)\d{9}$/;;
            let phoneNumInput = $('.newPassPhoneIpt').val();
            console.log(phoneNumInput);
            if (!pattern.test(phoneNumInput)) {
                console.log('手机号格式有错误');
                $('#phone').text("手机号格式有错误")
            } else {
                verFlag = true
            }
            if (verFlag) {
                let res = Interceptor('/send', 'post', {
                    'phone': phoneNumInput
                })
            }
        });
        $("#changePasswd").mousedown(function() {
            $("#submit").mousedown(function() {
                let codeText = /^\d{4}$/;
                let codeFlag = false;
                let codeVal = $("#codeVal").val();
                if (!codeText.test(codeVal)) {
                    codeFlag = false;
                    alert("验证码格式不对");
                } else {
                    codeFlag = true;
                }
            });
            var passFlag = false;
            let passText = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,10}$/;
            let passVal = $(".intelephonePass").val();
            if (!passText.test(passVal)) {
                passFlag = false;
                alert("密码至少包含大写字母，小写字母，数字，应在6-10位");
            } else {
                passFlag = true;
                Interceptor('/securityPass', 'post', {
                    'phone': $('.newPassPhoneIpt').val(),
                    'code': $("#codeVal").val(),
                    'editPass': passVal
                })
                alert('修改成功');
                window.location.reload();
            }

        });
    }
    changePhones();
    var MatterIntrVal = Interceptor('/MatterIntr', 'post');
    const headInformation = `  
            <div class ="contentName clearfix" style ="display: flex;">
                <div style="width:92px;height:92px;">
                    <img src="${MatterIntrVal.data[0].header}" class="headImg" id="pic">
                </div>
                    <p class="peopleName" style="margin-left:50px;">${MatterIntrVal.data[0].username}</p>
                    <div>
                        <span class="peopleThing">人事</span>
                    </div>
            </div>
            <div class="changbottom">
                <div id="changeImg">
                <form class="change_header_img" method="post" enctype="multipart/form-data">
                    <img src="https://19jwshow.oss-cn-shenzhen.aliyuncs.com/recruit/icon/pen.png " alt=" ">
                    <div id="filebutton">点击修改图片</div>
                    <input type="file" id="transmission" style="display: none;" accept="image/*">
                </form>
                </div>
                <div><a href="../views/PublishPosition.html" class="committing">发布职位</a></div>
            </div>
    `
    $(".rightName").append(headInformation);

    $("#filebutton").click(function() {
        $("#transmission").click(); //隐藏了input:file样式后
        $("#transmission").on("change", function() {
            let form = document.querySelector('.change_header_img');
            const file = this.files[0]
            const fileSize = 2 * 1024 * 1024; //图片最大
            let formData = new FormData(form)
            formData.append('headimg', file)
            if (file.size > fileSize) {
                alert('上传失败，请上传不大于2M的图片！', '')
                return
            } else {
                let headImg = upload('/headImg', 'post', formData);
                const {
                    code
                } = headImg
                if (code != 666) {
                    alert('上传失败')
                } else {
                    alert('上传成功')
                    window.location.reload();
                }
                $('#pic').upload('src', formData.data);
            }

        });
    });

    let companyInformation = Interceptor('/companyInfr', 'post');
    if (companyInformation.data != null) {
        const comInformation = `
    <p class="comname"><span>公司名:</span><span>${companyInformation.data.company_name}</span></p>
    <p class="comtype"><span>公司类型:</span><span>${companyInformation.data.type}</span></p>
    <p class="comnum"><span>公司职位数量:</span><span>${companyInformation.data.com_official}</span><span>个职位</span></p>
    <p class="comadress"><span>公司地址:</span><span>${companyInformation.data.company_area}</span></p>
    <p class="comPro">${companyInformation.data.company_name}:</p>
    <p class="CompanyProfile ">  ${companyInformation.data.company_introduction}</p>
    `
        $('.companyIn1').append(comInformation);
    } else {

        const noting = `
            <p>暂无更多信息</p>

        `
        $('.companyIn1').append(noting);
    }

    $('.changeMessage').mousedown(function() {
        $('.companyIn1').css("display", "none");
        $('.changeCompany1').css("display", "block");
    });

    $('.companysucess').click(function() {
        let comName = $('#workName').val();
        let workTyle = $('#workTyle').val();
        let workNum = $('#workNum').val();
        let introduction = $('#introduction').val();
        Interceptor('/infrChange', 'post', {
            'comName': comName,
            'comNum': workNum,
            'comType': workTyle,
            'comIntr': introduction

        });
        window.location.reload();
    });

    var produce = Interceptor('/companyProduct', 'post');

    function comLoop() {
        for (i = 0; i < produce.data.length; i++) {
            let companypro = `     
        <div class="product1 ">
            <div class="productBack"style="width:134px;height:134px">
                <img src = "${produce.data[i].product_img}"style = "width:100%;height:100%;object-fit:cover;" >
            </div>
            <div class="productList"style="margin-left:60px">
                <p class="productTitle ">${produce.data[i].coproduct}</p>
                <div class="secordTitle ">
                    <p class="productSmallTitle ">${produce.data[i].productType}</p>
                    <span class="smallProduct ">${produce.data[i].coproduct}</span>
                </div>
                <p class="detailProduct ">
                    ${produce.data[i].product_introduction}
                </p>
            </div>
        </div>
        `
            $('.companyIn2').append(companypro);
        }
    }
    comLoop();


    var companyImg = Interceptor('/ComImg', 'post');

    function comImgloop() {
        if (companyImg.data.length != 0) {
            for (i = 0; i < companyImg.data.length; i++) {
                if (companyImg.data[i].coenviron_img == null) {
                    const nullPhoto = `
                    <img src = "${companyImg.data[i].coenviron_img}" style="display:none">
                    `
                    $('.roomPhoto').append(nullPhoto);
                } else {
                    let comImgShow = `
            <div style="width:1020px;height:222px;margin-bottom:30px">
            <img src="${companyImg.data[i].coenviron_img}" alt="">
            </div>
            `
                    $('.roomPhoto').append(comImgShow);
                }
            }
        } else {
            const nothing = `
            <div style="width:1020px;height:80px;background-color:#fff;">
                <p style = "font-size:20px;line-height:80px;text-align:center;">暂无公司环境图片</p>
            </div>
                `
            $('.roomPhoto').append(nothing);
        }

    }
    comImgloop();
    var poatName = Interceptor('/matter', 'post');

    function EmployedPersonnel() {
        for (i = 0; i < poatName.data.length; i++) {
            const EmpPersonnel = `
            <div class="post_name">
                <div class="post_nameLeft">
                    <img src="${poatName.data[i].header}" alt=" " class="peopleHeader ">
                    <div class="people_content ">
                        <p class="peopleName">${poatName.data[i].username}</p>
                        <p class="position">${poatName.data[i].official}</p>
                    </div>
                </div>
            </div>
            `

            $('.EmpPersonnel').append(EmpPersonnel);
        }
    }
    EmployedPersonnel();



    $('.add').click(function() {
        $('.changeBenefitsContent').append('<input type="text " name="myfun" class="guarantee" maxlength="15" style="margin-bottom: 20px;padding-left: 15px;"><br>')
    });
    var officalOk = Interceptor('/official', 'post');

    for (i = 0; i < officalOk.data.length; i++) {
        const offcontent = `
        <div class="posstionCard">
            <p class="posstionName">${officalOk.data[i].official}</p>
            <span class="orangeSize">${officalOk.data[i].job_money}薪</span>
            <a href="#"><span class="delect ">删除</span></a>
        </div>
    `
        $('.companyPosstion').append(offcontent);
        $('.delect').mousedown(function() {
            $(this).closest('div').remove();
            Interceptor('/change', 'post', {
                'off': officalOk.data[i].official,
                'jobMoney': officalOk.data[i].job_money
            })
        });
    }

    function deloop() {
        var delectoff = Interceptor('/deLe', 'post');
        for (i = 0; i < delectoff.data.length; i++) {
            const offcontent = `
        <div class="posstionCard ">
            <p class="posstionName ">${delectoff.data[i].official}</p>
            <span class="orangeSize ">${delectoff.data[i].job_money}薪</span>
        </div>
    `
            $('.comDeleted').append(offcontent);
        }
    }
    deloop();
    $('#Phonesubmit').click(() => {
        let newPassPhoneIptVal = $('.newPassPhoneIpt').val();
        let phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        let phoneFlag = false;

        let passCode = $('.passCode');
        let passCodeVal = passCode.val();
        let codeTest = /^\d{4}$/;
        let codeFlag = false;

        let modifyPassIpt = $('.modifyPassIpt');
        let modifyPassIptVal = modifyPassIpt.val();
        let passTest = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
        let passFlag = false;

        if (!codeTest.test(passCodeVal)) {
            alert('验证码格式不正确');
            codeFlag = false;
        } else {
            codeFlag = true;
        }
        if (!phoneTest.test(newPassPhoneIptVal)) {
            alert('手机号格式不正确')
            phoneFlag = false;
        } else {
            phoneFlag = true;
        }
        if (!passTest.test(modifyPassIptVal)) {
            alert('密码格式为任意数字字母，6-12位')
            passFlag = false;
        } else {
            passFlag = true;
        }
        if (phoneFlag == codeFlag == passFlag) {
            Interceptor('/securityPass', 'post', {
                'phone': newPassPhoneIptVal,
                'code': passCodeVal,
                'editPass': modifyPassIptVal
            });
        }
    });

    function employLoop() {
        var employPosition = Interceptor('/employPosition', 'post');
        for (i = 0; i < employPosition.data.length; i++) {
            const employ = `
                 <div class="posstionCard">
                     <p class="posstionName">${employPosition.data[i].official}</p>
                     <span class="orangeSize">${employPosition.data[i].job_money}</span>
                 </div>
    `
            $('.comEmployed').append(employ);
        }
    }
    employLoop();
    $('.walefuLi').click(function() {
        function myfun() {
            var arr = [];
            $('input[name="myfun"]').each(function() {
                var res = $(this).val();
                arr.push(res);
            });
            Interceptor('/welfareChange', 'post', {
                'welfare[]': arr
            })
        }
        myfun();
        window.location.reload();
    });
    var maphoneal = Interceptor('/maPhone', 'post')

    $('.onlyonephone').html(maphoneal.data);

    function listBefits() {
        var wallet = Interceptor('/welfare', 'post');
        var inputWallet = wallet.data.company_welfare.split(',');
        for (i = 0; i < inputWallet.length; i++) {
            if (wallet.data != null) {
                var walLi = `
            <li>${inputWallet[i]}</li>
        `
                $('#listBefits').append(walLi);
            } else {
                const noting = `
            <li>暂未完善福利</li>
            `
                $('#listBefits').append(noting);
            }
        }
    }
    listBefits();
    let formData1 = ''
    let formData2 = ''

    function changePhoto() {
        $(".thingIngOne").mousedown(function() {
            $("#Propic").click(); //隐藏了input:file样式后
            $("#Propic").on("change", function() {
                let form = document.querySelector('.produceImgsecord');
                const file = this.files[0];
                const fileSize = 2 * 1024 * 1024; //图片最大
                if (file.size > fileSize) {
                    alert('图片不能超过2M');
                    return;
                } else {
                    const src1 = window.URL.createObjectURL(this.files[0]);
                    $('#proImg1').attr('src', src1);
                    $('#proImg1').css({
                        'display': 'block',
                        'width': '100%',
                        'height': '100%',
                        'border-radius': '50%',
                        'object-fit': 'cover'
                    });
                    $('.onesize').hide();
                    formData1 = file
                }
                //转成可以在本地预览的格式
            });
        });
    }
    changePhoto();


    function changePhoto1() {
        $(".thingIngTwo").mousedown(function() {
            $("#Propic2").click(); //隐藏了input:file样式后
            $("#Propic2").on("change", function() {
                let form = document.querySelector('.produceImgFirst');
                const file = this.files[0];
                const fileSize = 2 * 1024 * 1024; //图片最大
                if (file.size > fileSize) {
                    alert('图片不能超过2M');
                    return;
                } else {
                    var src2 = window.URL.createObjectURL(this.files[0]);
                    // 将图片路径赋值给img中的src
                    $('#proImg2').attr('src', src2);
                    $('#proImg2').css({
                        'display': 'block',
                        'width': '100%',
                        'height': '100%',
                        'border-radius': '50%',
                        'object-fit': 'cover'
                    });
                    $('.Twosize').hide();
                    formData2 = file
                }
                //转成可以在本地预览的格式
            });
        });
    }
    changePhoto1();

    function judge(arr) {
        for (let item in arr) {
            if (arr[item] === undefined || arr[item] == '') {
                arr[item] = null;
            }
        }
    }

    function produceComit() {
        $('.produceSuceess').click(function() {
            changePhoto();
            changePhoto1();

            var produceNameval1 = $('#produceNameval1').val();
            var productype1 = $('#productype1').val();
            var produceThing1 = $('#produceThing1').val();
            var produceNameval2 = $('#produceNameval2').val();
            var productype2 = $('#productype2').val();
            var produceThing2 = $('#produceThing2').val();

            if (produceNameval1.length == 0) {
                var produceNameval1 = null;
            }
            if (productype1.length == 0) {
                var productype1 = null;
            }
            if (produceThing1.length == 0) {
                var produceThing1 = null;
            }
            if (produceNameval2.length == 0) {
                var produceNameval2 = null;
            }
            if (productype2.length == 0) {
                var productype2 = null;
            }
            if (produceThing2.length == 0) {
                var produceThing2 = null;
            }
            // if (formData1 == undefined) {
            //     var formData1 = null;
            // }
            console.log(formData2);
            if (formData2 == undefined) {
                console.log(111);
                var formData2 = null;
            }

            const productNameArr = [produceNameval1, produceNameval2];
            const productCategory = [productype1, productype2]
            const productBrief = [produceThing1, produceThing2]
            const productImg = [formData1, formData2]
            if (productNameArr[1], productCategory[1], productBrief[1], productImg[1] != null) {
                productFormData.append('product_img[]', formData2)
                productFormData.append('coproduct[]', produceNameval2)
                productFormData.append('productype[]', productype2)
                productFormData.append('product_introduction[]', produceThing2)

            }
            // console.log(productNameArr);


            const productFormData = new FormData();
            productFormData.append('product_img[]', formData1)
            productFormData.append('coproduct[]', produceNameval1)
            productFormData.append('productype[]', productype1)
            productFormData.append('product_introduction[]', produceThing1)
            upload('/productChange', 'post', productFormData);
            window.location.reload();
        });
    }
    produceComit();
    var phonenumber = Interceptor('/getUserAttr', 'post');

    function phoneDisplay() {
        const phonestar = phonenumber.data[0].phone;
        const phonefinish = phonestar.substr(0, 3) + '****' + phonestar.substring(7, 11);
        const phonenumdispaly = `
        ${phonefinish}
             `
        $('#onlyonephone').append(phonenumdispaly);
    }
    phoneDisplay();

    let companyEvent = [];
    $('#workPhoto1').on('change', function() {
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024;
        if (file.size > fileSize) {
            alert('图片不能超过2M');
            this.files[0] = ''
            return;
        } else {
            const Worksrc = window.URL.createObjectURL(this.files[0]);
            // 将图片路径赋值给img中的src
            $('.newPhoto').eq(0).attr('src', Worksrc);
            $('.newPhoto').eq(0).css('display', 'block')
            $('.workAddPhoto1').children().eq(1).hide();
            companyEvent.push(file)
            $(".trash").show()
        }
    })
    $(".trash").click(function() {
        $('.newPhoto').eq(0).css('display', 'none')
        $('.workAddPhoto1').children().eq(1).show();
        $(".trash").hide();
    })



    $("#workPhoto2").on('change', function() {
        const file = this.files[0]
        const fileSize = 2 * 1024 * 1024;
        const fileData = this.files[0];
        var srcwork2 = window.URL.createObjectURL(this.files[0]);
        if (fileData.size > fileSize) {
            alert('图片不能超过2M');
            this.files[0] = ''
            return;
        } else {
            const Worksrc = window.URL.createObjectURL(this.files[0]);
            // 将图片路径赋值给img中的src
            $('.newPhoto2').eq(0).attr('src', Worksrc);
            $('.newPhoto2').eq(0).css('display', 'block')
            $('.workAddPhoto2').children().eq(1).hide();
            companyEvent.push(file)
            $(".trash2").show()
        }
        $(".trash2").click(function() {
            $('.newPhoto2').eq(0).css('display', 'none')
            $('.workAddPhoto2').children().eq(1).show();
            $(".trash2").hide()

        });
    });
    console.log(companyEvent);
    $('.changeWork').click(function() {
        let allFromData = new FormData();
        judge(companyEvent);
        allFromData.append('image[]', companyEvent[0])
        allFromData.append('image[]', companyEvent[1])

        upload('/postComImg', 'post', allFromData);
        window.location.reload();
    })

    function checkBoxPeople() {
        var cheakBox = Interceptor('/matterCheck', 'post');
        for (i = 0; i < cheakBox.data.length; i++) {
            const cheakPeople = `
        <div class="post_name">
            <div class="post_nameLeft"> <img src="${cheakBox.data[i].header}" alt=" " class="peopleHeader ">
                <div class="people_content">
                    <p class="peopleName">${cheakBox.data[i].username}</p>
                    <p class="position">${cheakBox.data[i].exp_job}</p>
                </div>
            </div>
            <div class="cheak">
                <a href="JavaScript:;" class="cheakBtn adpot ">通过</a>
                <a href="JavaScript:;" class="cheakBtn refust ">拒绝</a>
            </div>
        </div>
    `
            $('.cheakPeopleBox').append(cheakPeople);
            var userId = cheakBox.data[i].user_id;
            $('.adpot').mousedown(function() {
                Interceptor('/matterOr', 'post', {
                    'userId': userId,
                    'check': 1
                });
                $(this).css({
                    background: "#FF511C",
                    color: "#fff"
                });
                $(this).html('已通过');
                $(this).siblings().css('pointer-events', 'none');
                window.location.reload()
            });
            $('.refust').mousedown(function() {
                Interceptor('/matterOr', 'post', {
                    'userId': userId,
                    'check': 0
                });
                $(this).css({
                    background: "#FF511C",
                    color: "#fff"
                });
                $(this).html('已拒绝');
                $(this).siblings().css('pointer-events', 'none');
                window.location.reload()
            });

        }

    }
    checkBoxPeople();
});